// RealPlay Ltd  copyrights.
// This program is Open Source AsIs
// You may use it for all purposes, including commercial
// But!!!
// If you use any part of this code, you must state clearly
// both in your source file and in the Help|about of your program,
// that the origin program or code was built by RealPlay Ltd

// ***********************************************************************/
// *                                                                     */
// * This unit is flip & Rot dialog (Ctrl-R) for  "Paint" Program        */
// *                                                                     */
// ***********************************************************************/

unit UPflip;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, ExtCtrls;

type
  TFflip = class(TForm)
    Button1: TButton;
    Button2: TButton;
    GroupBox1: TGroupBox;
    RHor: TRadioButton;
    RVer: TRadioButton;
    RRot: TRadioButton;
    PRot: TPanel;
    R90: TRadioButton;
    R180: TRadioButton;
    R270: TRadioButton;
    RCustom: TRadioButton;
    SDeg: TSpinEdit;
    procedure RHorClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SDegClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Fflip: TFflip;

implementation

uses UPMain;
{$R *.dfm}

Type
  pixinRow = Array [0 .. 128000] Of TRGBTriple;

Function BitmapFlip(Const Vertical: Boolean; Const Horizontal: Boolean;
  const BitmapIn: TBitmap; BitmapOut: TBitmap): Boolean;
Var
  DataIn: ^pixinRow;
  DataOut: ^pixinRow;
  inRow: Integer;
  inCol: Integer;
Begin
  Result := False;
  Try
    If BitmapIn.PixelFormat <> pf24bit Then
      Exit;
    With BitmapOut Do
    Begin
      Width := BitmapIn.Width;
      Height := BitmapIn.Height;
      PixelFormat := BitmapIn.PixelFormat;
      TransparentColor := BitmapIn.TransparentColor;
    End;
    For inRow := 0 To BitmapIn.Height - 1 Do
    Begin
      DataIn := BitmapIn.ScanLine[inRow];
      If Vertical then
      begin
        DataOut := BitmapOut.ScanLine[BitmapIn.Height - 1 - inRow];
        move(DataIn^, DataOut^, BitmapOut.Width * sizeof(TRGBTriple));
      end;
      if Horizontal then
      Begin
        DataOut := BitmapOut.ScanLine[inRow];
        For inCol := 0 To BitmapIn.Width - 1 Do
          DataOut^[inCol] := DataIn^[BitmapIn.Width - 1 - inCol];
      End;
    End;
    Result := True;
  Except
  End;
End;

procedure enableAll(ac: Twincontrol; b: Boolean);
var
  i: Integer;
begin
  for i := 0 to ac.ControlCount - 1 do
    ac.Controls[i].Enabled := b;
end;

procedure TFflip.RHorClick(Sender: TObject);
begin
  if RRot.Checked then
    enableAll(PRot, True)
  else
    enableAll(PRot, False);
end;

procedure TFflip.Button1Click(Sender: TObject);
var
  Nb, PBM: TBitmap;
  tmpDeg: Integer;
begin
  with Fpaint.FWA do
  begin
    with ImSelS do
      if RRot.Checked then
      begin
        Degrees := SDeg.Value;
        if R90.Checked then
          Degrees := 90;
        if R180.Checked then
          Degrees := 180;
        if R270.Checked then
          Degrees := 270;
        tmpDeg := Degrees;
        If Visible then
          DrawRotate
        else
        begin
          // tmpCur:=screen.Cursor;
          screen.Cursor := crHourGlass;
          selectall;
          hide;
          Degrees := tmpDeg;
          DrawRotate;
          show;
          PaintSel;
          clearSel;
        end;
      end
      else
      begin
        Nb := TBitmap.create;
        If ImSelS.Visible then
          PBM := ImSelS.SelSource
        else
          PBM := Im.picture.Bitmap;
        try
          if RHor.Checked then
            BitmapFlip(False, True, PBM, Nb);
          if RVer.Checked then
            BitmapFlip(True, False, PBM, Nb);
          PBM.Assign(Nb);
          If ImSelS.Visible then
            ImSelS.MarkBlueDotsAndShowSel;
        finally
          Nb.Free;
        end;
      end;
    MyStat.Changed := True;
  end;
  close;
end;

procedure TFflip.SDegClick(Sender: TObject);
begin
  with Fpaint.FWA.ImSelS do
    If Visible then
      try
        Degrees := SDeg.Value;
        DrawRotate;
      except
      end;
end;

procedure TFflip.Button2Click(Sender: TObject);
begin
  close;
end;

procedure TFflip.FormCreate(Sender: TObject);
begin
  top := Fpaint.top + 60;
  if top + Height + 20 > screen.Height then
    top := screen.Height - Height - 20;
  left := Fpaint.left + 20;
  if left < 20 then
    left := 20;
  if left + Width + 20 > screen.Width then
    left := screen.Width - Width - 20;
end;

end.
